<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<link rel="stylesheet" media="screen" href="${ctx}/css/Tree.css"/>
<!-- jQplot CSS END -->

<script type="text/javascript" src="${ctx}/js/jquery.itextclear.js"></script>
<script type="text/javascript" src="${ctx}/js/Tree.js"></script>
<script type="text/javascript">
    $(document)
            .ready(
            function () {
                $(
                        'input[type=text], input[type=password], input[type=url], input[type=email], input[type=number], textarea',
                        '.form').iTextClear();
            }
    );
</script>
<!-- jQplot CSS -->


<!-- DATATABLES CSS -->
<link rel="stylesheet" media="screen"
      href="${ctx}/lib/datatables/css/vpad.css"/>
<script type="text/javascript"
        src="${ctx}/lib/datatables/js/jquery.dataTables.js"></script>
<!-- DATATABLES CSS END -->


<h1 class="page-title">新员工招聘申请 - ${title}</h1>

<div class="container_12 clearfix leading">
    <div class="side grid_12 leading">
        <form class="form has-validation" id="post_form">
            <s:if test="#request.inviteApply.id > 0">
                <input type="hidden" name="inviteApply.id" value="${inviteApply.id}"/>
            </s:if>
            <div class="clear"></div>
            <div class="clearfix">
                <label class="form-label">需求单位 <em>*</em></label>

                <div class="form-input">
                    <select id="org_sel" name="inviteApply.organization.id"></select>
                </div>
            </div>
            <div class="clearfix">
                <label class="form-label">职务 <em>*</em></label>

                <div class="form-input">
                    <select id="duty_sel" name="inviteApply.duty.id"></select>
                </div>
            </div>
            <div class="clearfix">
                <div class="clearfix">
                    <label class="form-label">职等</label>

                    <div class="form-input">
                        <select id="grade_sel" name="inviteApply.grade.id"></select>
                    </div>

                    <div class="clearfix">
                        <label class="form-label">职级</label>

                        <div class="form-input">
                            <select id="gv_sel" name="inviteApply.gradeLevel.id"></select>
                        </div>


                        <div class="clearfix">
                            <label class="form-label">人数 <em>*</em></label>

                            <div class="form-input">
                                <input type="text" id="apply_amount"
                                       name="inviteApply.applyAmount" required="required"
                                       placeholder="请输入需求人数" value="${inviteApply.applyAmount}"/>
                            </div>
                        </div>

                        <div class="clearfix">

                            <label class="form-label">岗位招聘需求时间 <em>*</em></label>

                            <div class="form-input">
                                <fmt:formatDate value="${inviteApply.applyDate}" var="applydate"
                                                pattern="yyyy-MM-dd"/>
                                <input type="text" id="apply_date" name="apply_date"
                                       required="required" value='${applydate}'
                                       onclick="WdatePicker()"/>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                    <div class="form-action clearfix">
                        <button class="button" type="button" onclick="checkForm()">
                            <s:if test="#request.inviteApply.id > 0">修改</s:if>
                            <s:else>添加</s:else>
                        </button>
                        <button class="button" type="reset">重置</button>
                        <a class="button" href="javascript:void(0)"
                           onclick="javascript:history.back()"><span
                                style="background: url(${ctx}/images/img/arrow_undo.png)" class="icon">&nbsp;</span>返回</a>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="clear"></div>
<script type="text/javascript">
	var strOrgHTML = "";
	var frOrgId = ${organizationId};
	function buildOrgString(data){
		$.each(data, function (i, org) {
			if(org.id == frOrgId){
				strOrgHTML += "<option value=\"" + org.id + "\" selected='selected'>" + org.name + "</option>";
			}else{
				strOrgHTML += "<option value=\"" + org.id + "\">" + org.name + "</option>";
			}

        	if(org.children != null && org.children.length > 0){
        		buildOrgString(org.children);
        	}
        });
	}
    $(document).ready(function () {
    	//bind org data
    	strOrgHTML = "";
    	$("#org_sel").html("");
        $("#org_sel").prev().html("");
    	$.ajax({
            type:"get",
            url:"/ajax/org_serlist.xhtml",
            //data: "name=John&location=Boston",
            dataType:"json",
            success:function (data) {
            	//var data = eval("(" + vdata + ")");
                if (data != null && data.length > 0) {
                    $.each(data, function (i, org) {
                    	strOrgHTML += "<option value=\"" + org.id + "\"" + (i == 0 && frOrgId <= 0 ? " selected='selected'" : "") + ">" + org.name + "</option>";
                    	if(org.children != null && org.children.length > 0){
                    		buildOrgString(org.children);
                    	}
                    });

                    $("#org_sel").append(strOrgHTML);
                    $("#org_sel").prev().html($("#org_sel").find("option:selected").text());
                }
                // load the grade list
                loadDutyByOrgId($("#org_sel").val());
            }
        });
        //bind event to org_sel
        $("#org_sel").change(function () {
            loadDutyByOrgId($("#org_sel").val());
        })
        $("#duty_sel").change(function () {
            loadGradeByDutyId($("#duty_sel").val());
        });
        $("#grade_sel").change(function () {
            loadGradeLevelByDutyId($("#duty_sel").val(), $("#grade_sel").find("option:selected").text());
        });
    });
    /**
     * load the duty list
     * */
    function loadDutyByOrgId(orgId) {
        //load the duty
        $("#duty_sel").html("");
        $("#duty_sel").prev().html("");
        if(orgId == null){
        	return;
        }
        $.ajax({
            type:"get",
            url:"/ajax/duty-list.xhtml?organizationId=" + orgId,
            //data: "name=John&location=Boston",
            dataType:"json",
            success:function (data) {
                var strHTML = "";
                if (data != null && data.length > 0) {
                    $.each(data, function (i, duty) {
                        if(i == 0 && ${dutyId} <= 0){
                            strHTML += "<option value=\"" + duty.id + "\"" + (i == 0 ? " selected='selected'" : "") + ">" + duty.dutyName + "</option>";
                        }else if(${dutyId} > 0 && duty.id == ${dutyId}){
                            strHTML += "<option value=\"" + duty.id + "\" selected='selected'>" + duty.dutyName + "</option>";
                        }else{
                            strHTML += "<option value=\"" + duty.id + "\">" + duty.dutyName + "</option>";
                        }

                    });

                    $("#duty_sel").append(strHTML);
                    $("#duty_sel").prev().html($("#duty_sel").find("option:selected").text());
                }
                // load the grade list
                loadGradeByDutyId($("#duty_sel").val());
            }
        });
    }

    /**
     * load the grade level
     */
    function loadGradeByDutyId(dutyId) {
        $("#grade_sel").html("");
        $("#grade_sel").prev().html("");
        if(dutyId == null){
        	return;
        }
        $.ajax({
            type:"get",
            url:"/ajax/grade_list.xhtml?dutyId=" + dutyId,
            //data: "name=John&location=Boston",
            dataType:"json",
            success:function (data) {
                var strHTML = "";
                if (data != null && data.length > 0) {
                    $.each(data, function (i, grade) {
                        if(i == 0 && ${gradeId} <= 0){
                            strHTML += "<option value=\"" + grade.id + "\" selected='selected'>" + grade.gradeName + "</option>";
                        }else{
                            strHTML += "<option value=\"" + grade.id + "\"" + (grade.id == ${gradeId} ? " selected='selected'" : "") + ">" + grade.gradeName + "</option>";
                        }

                    });

                    $("#grade_sel").append(strHTML);
                    $("#grade_sel").prev().html($("#grade_sel").find("option:selected").text());
                }
                //load the grade level list
                loadGradeLevelByDutyId($("#duty_sel").val(), $("#grade_sel").find("option:selected").text());
            }
        });
    }

    /**
     * 加载职级列表
     */
    function loadGradeLevelByDutyId(dutyId, gradeName) {
        $("#gv_sel").html("");
        $("#gv_sel").prev().html("");
        $.ajax({
            type:"POST",
            url:"/ajax/grade_lvlist.xhtml",
            data:{"dutyId":dutyId, "gradeName":gradeName},
            dataType:"json",
            success:function (data) {
                var strHTML = "";
                if (data != null && data.length > 0) {
                    $.each(data, function (i, grade) {
                        if(i == 0 && ${gradeLvId} <= 0){
                            strHTML += "<option value=\"" + grade.id + "\" selected='selected'>" + grade.gradeSort + "</option>";
                        }else{
                            strHTML += "<option value=\"" + grade.id + "\"" + (grade.id == ${gradeLvId} ? " selected='selected'" : "") + ">" + grade.gradeSort + "</option>";
                        }
                    });
                    $("#gv_sel").append(strHTML);
                    $("#gv_sel").prev().html($("#gv_sel").find("option:selected").text());
                }
            }
        });
    }

    /**
     * 返回页面
     */
    function returnPage() {
        this.location.href = "#invite/invite_apply_list.xhtml?org_id=" + $("#org_sel").value();
    }
    function checkForm() {
        if ($("#org_sel").children().length == 0) {
            alert("未选择组织结构，请先添加组织结构");
            return;
        }
        if ($("#duty_sel").children().length == 0) {
            alert("该组织结构没有职务，请先添加职务");
            return;
        }

        if ($("#apply_amount").val() == null || $("#apply_amount").val().trim() == "") {
            alert("请填写招聘人数");
            return;
        }
        if (isNaN($("#apply_amount").val())  || Number($("#apply_amount").val()) <= 0) {
            alert("请正确填写招聘人数");
            return;
        }
        if ($("#apply_date").val() == null || $("#apply_date").val().trim() == "") {
            alert("请填写招聘日期");
            return;
        }

        var url = "${ctx}/invite/post_apply.xhtml";
        $.ajax(url, {
            type:"POST",
            data:$("#post_form").serialize(),
            dataType:"html",
            cache:false,
            success:function (a) {
                if (confirm('还要继续添加吗？')) {
                    document.getElementById("post_form").reset();
                } else {
                    location.href = "${ctx}/index.xhtml#invite/invite_list.xhtml?p=apply";
                }
            },
            error:function (XMLHttpRequest, textStatus, errorThrown) {
                alert('提交申请发生异常');
            },
            complete:function () {
            }
        });
    }
</script>
